from os import path
from collections import Counter
import jieba
import matplotlib.pyplot as plt
from wordcloud import WordCloud, ImageColorGenerator

text_read =open('xinlang.txt', 'r', encoding='utf-8').read()
# jieba.load_userdict('test_dic.txt')
c = jieba.cut(text_read, cut_all=False, HMM=True)
word_list = []
for i in c:
    word_list.append(i)
word_dic = {}
for i in word_list:
    if i not in word_dic:
        word_dic[i] = 1
    else :
        word_dic[i] += 1
over_list = sorted(word_dic.items(), key = lambda x : x[1], reverse=True)

wc = WordCloud(background_color="white",
               max_words=1000,
               font_path="./simkai.ttf",  # 使用字体
               max_font_size=100,
               width=1000,height=860, margin=2)
wc.fit_words(dict(over_list[20:]))
plt.figure()
plt.imshow(wc)
plt.axis("off")
plt.show()
